package com.y.fund.utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Set;

public class FileUtil {

	/** Suffix of hazardous documents */
	public static final Set<String> dangerSuffixs = new HashSet<String>();//危险文件的后缀

	/** Suffix of picture file */
	public static final Set<String> imgSuffixs = new HashSet<String>(); 

	static{
		dangerSuffixs.add(".exe");
		dangerSuffixs.add(".js");
		dangerSuffixs.add(".bat");
		dangerSuffixs.add(".sh");

		imgSuffixs.add(".jpg");
		imgSuffixs.add(".png");
		imgSuffixs.add(".jpeg");
	}

	/** Get a suffix of the file path */
	public static String GetSuffix(final String filePath){
		if(null==filePath || "".equals(filePath)){
			return "";
		}
		if(filePath.indexOf(".") == -1){
			return "";
		}
		return filePath.substring(filePath.lastIndexOf("."));
	}
	
	/** Judge whether it is a dangerous file according to the suffix of the file name */
	public static boolean IsDangerFile(String fileName){
		if(null==fileName){
			return false;
		}
		String fn = fileName.trim().toLowerCase();
		int fdot =  fn.lastIndexOf(".");
		if(fdot > 0) {
			fn = fn.substring(fdot);
		}
		
		return dangerSuffixs.contains(fn);
	}

	/** Generate a relative upload directory, excluding the previous absolute path */
	public static String NewRelativeDir(){  
		return new SimpleDateFormat("yyyyMM/ddHH").format(System.currentTimeMillis());
	}
	
	/** Obtain the complete access path 
	 * (including the access protocol (such as http:// etc.) according to the relative path where the file is located */
	public static String ConcatPath(String rootPath, String relativePath){
		if(null==relativePath) {
			relativePath = "";
		}
		String rp = (StrUtil.isBlankIncNullStr(rootPath)?"":rootPath);
		if(rp.endsWith("\\") || rp.endsWith("/")) {
			return rp.concat((relativePath.startsWith("/") || relativePath.startsWith("\\"))? relativePath :relativePath);
		}else {
			return rp.concat((relativePath.startsWith("/") || relativePath.startsWith("\\"))?"":"/").concat(relativePath);
		}
	}

    /** Create a parent folder based on the full path of the file */
    public static boolean MkParentDirsByFullFilePath(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            return true;
        }
        File fparent = file.getParentFile();
        if (!fparent.exists() || !fparent.isDirectory()) {
        	return fparent.mkdirs();
        }
        return true;
    }
	
    /** Delete files, or all files in a folder */
    public static boolean delete(String fileFullName) {
    	try {
            File file = new File(fileFullName);
            if (!file.exists()) {
                return false;
            } else {
                if (file.isFile())
                    return deleteSingleFile(fileFullName);
                else
                    return deleteDirectory(fileFullName, false);
            }
    	}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
    }

    /** Delete a single file */
    protected static boolean deleteSingleFile(String fileName) {

        File file = new File(fileName);
        if (file.exists() && file.isFile()) {
            return file.delete();
        } else {
            return false;
        }
    }

    /** Delete the directory and the files under it */
    protected static boolean deleteDirectory(String dir, boolean deleteDir) {
        if (!dir.endsWith(File.separator))
            dir = dir + File.separator;
        File dirFile = new File(dir);
        if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
            return false;
        }
        boolean flag = true;
        File[] files = dirFile.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                flag = FileUtil.deleteSingleFile(files[i].getAbsolutePath());
                if (!flag)
                    break;
            }
            else if (files[i].isDirectory()) {
                flag = FileUtil.deleteDirectory(files[i].getAbsolutePath(), deleteDir);
                if (!flag)
                    break;
            }
        }
        if (!flag) {
            return false;
        }
        if (deleteDir) {

            return dirFile.delete();
        }

        return true;
    }

}
